﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using LY.Common;
using ServiceStack.Text;
using LY.Entity.Entities;
using LY.Entity;

namespace LY.Extension
{
    /// <summary>
    /// Controller 基类，包装一些同样的方法和属性
    /// </summary>
    //[LY.Extension.MyAuthorizeAttribute]
    public class MyControllerBase : Controller
    {   

        /// <summary>
        /// 获取当前登录用户标识
        /// </summary>
        /// <value>The user ID.</value>
        protected virtual string UserId
        {
            get
            {
                return MyContext.Identity;
            }
        }

        ///// <summary>
        ///// 当前登录用户信息
        ///// </summary>
        ///// <value>The current user.</value>
        protected virtual U_Member CurrentUser
        {
            get
            {
                return MyContext.CurrentLoginUser;
            }
        }
        /// <summary>
        /// 身份证号码
        /// </summary>
        protected virtual string IDCode
        {
            get
            {
                return CurrentUser == null ? "" : MyContext.CurrentLoginUser.IDCode;
            }
        }

        ///// <summary>
        ///// 当前登录用户手机号码
        ///// </summary>
        ///// <value>The current user.</value>
        protected virtual string Mobilenumber
        {
            get
            {
                return CurrentUser == null ? "" : MyContext.CurrentLoginUser.Mobilenumber;
            }
        }
        protected virtual bool? IsIDValidate
        {
            get
            {
                return CurrentUser == null ? false : MyContext.CurrentLoginUser.IsIDValidate;
            }
        }
        protected virtual bool? IsCardValidate
        {
            get
            {
                return CurrentUser == null ? false : MyContext.CurrentLoginUser.IsCardValidate;
            }
        }

        protected virtual bool? IsBankValidate
        {
            get
            {
                return CurrentUser == null ? false : MyContext.CurrentLoginUser.IsBankValidate;
            }
        }

        /// <summary>
        /// 获取当前登录用户ID
        /// </summary>
        /// <value>The current user.</value>
        protected virtual int CurrentLoginUserId
        {
            get
            {
                return CurrentUser == null ? 0 : MyContext.CurrentLoginUser.ID;
            }
        }

        /// <summary>
        /// 获取当前登录用户账号 用户名
        /// </summary>
        /// <value>The current user.</value>
        protected virtual string CurrentLoginUserNo
        {
            get
            {
                return CurrentUser == null ? "" : MyContext.CurrentLoginUser.Name;
            }
        }

        protected virtual string RealName
        {
            get
            {
                return CurrentUser == null ? "" : MyContext.CurrentLoginUser.RealName;
            }
        }

        /// <summary>
        /// 获取当前登录用户真实姓名
        /// </summary>
        /// <value>The current user.</value>
        protected virtual string CurrentLoginRealName
        {
            get
            {
                return CurrentUser == null ? "" : MyContext.CurrentLoginUser.RealName;
            }
        }
        /// <summary>
        /// 获取当前登录邮箱
        /// </summary>
        /// <value>The current user.</value>
        protected virtual string CurrentLoginUserEmail
        {
            get
            {
                return CurrentUser == null ? "" : MyContext.CurrentLoginUser.Email;
            }
        }
        /// <summary>
        /// 当前登录会员手机号131******202
        /// </summary>
        protected virtual string Enc_Mobilenumber
        {
            get
            {
                string newMobileNumber = "";
                if (CurrentUser == null)
                {
                    return "";
                }
                else
                {
                    newMobileNumber = MyContext.CurrentLoginUser.Mobilenumber.Substring(0, 4) + "****" + MyContext.CurrentLoginUser.Mobilenumber.Substring(8, 3);
                }
                return newMobileNumber;
            }
        }

        ///// <summary>
        ///// 当前登录会员姓+Levin***
        ///// </summary>
        //protected virtual string Enc_MemberName
        //{
        //    get
        //    {
        //        string newEnc_MemberName = "";
        //        if (CurrentUser == null)
        //        {
        //            return "";
        //        }
        //        else
        //        {
        //            newEnc_MemberName = MyContext.CurrentLoginUser.RealName.Substring(0,1) + "****";
        //        }
        //        return newEnc_MemberName;
        //    }
        //}

        //重写基类的方法
        #region 重写基类的方法
        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new ServiceStackJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };
        }
        #endregion
    }

    public class ServiceStackJsonResult : JsonResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";

            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }

            if (Data != null)
            {
                response.Write(Data.ToJson());
            }
        }
        
        
    }

}
